package com.xtm.exercise.base.A12threadDemo.createThread.Create04ThreadPool;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * 创建线程池的方式：
 *  方式一：Executors
 *      ExecutorService service = Executors.newFixedThreadPool(10);
 *  方式二：
 */
public class DemoThreadPool {


    //创建线程池，有10个空闲的线程
    public static ExecutorService service = Executors.newFixedThreadPool(10);

    //创建线程方式一  Executors
    @Test
    public void test01(){
        //当前系统中池有一两个，每个异步任务，提交给线程池让他自己去执行就行
        service.execute(new Demo01ThreadPool.RunnableImpl());

        //另外几种创建线程池
//        ExecutorService pool = Executors.newCachedThreadPool(); core是0，所有都可能回收
//        ExecutorService pool = Executors.newFixedThreadPool(); 固定大小，core=max，都不可回收
//        ScheduledExecutorService pool = Executors.newScheduledThreadPool(); 定时任务的线程池
//        Executors.newSingleThreadExecutor()单线程的线程池，后台从队列里面获取任务，挨个执行

    }

    //创建线程方式二
    @Test
    public void test02(){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        System.out.println("main...end...");
    }


}
